<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="database_layer.*" %>
<%@ page import="interface_layer.*" %>
<%@ page import="processing_layer.*" %>
<%@ page import="java.util.*" %>
<%@ page errorPage="exceptionPage.jsp" %>
<%-- 
	This JSP presents a specific course details page. It contains the relevant information
	about the course, along with a set of sessions. In case you're an admin and it's his course
	he can delete it, in case it's a student he can register / unregister (if the conditions apply)
 --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255"/>
<link href="css/templatemo_style.css" rel="stylesheet" type="text/css" />
<title>Course details</title>
</head>
<body>
<jsp:include page="header_part_1.html"/>
            <div id="templatemo_menu">
                <ul>
                    <li><a href="index.jsp">Home</a></li>
                    <li><a href="about.jsp">About Us</a></li>
<%
                    	String username = (String) session.getAttribute("username");
                        if (null != username)
                        {
%>
                            <li><a href="userPage.jsp">Your profile</a></li>
                            <li><a href="allCourses.jsp" class="current">Courses</a></li>
                    		<li><a href="allUsers.jsp">Users</a></li>
<%
                        }
%>
                </ul>    	
            </div> <!-- end of templatemo_menu -->
<jsp:include page="header_part_2.html"/>
<div id="content">
<%
	if (null == username)
	{
		/* Not logged in = Not authorized */
		ErrorHandler.parseForErrors(Common.UNAUTHORIZED_OPERATION, response, null);
		return;
	}

	String requestedCourse = request.getParameter("course");
	if (null == requestedCourse)
	{
		ErrorHandler.parseForErrors(Common.BAD_PARAMETERS, response, null);
		return;
	}
	
	Course c = QueryDispatcher.getCourseDetails(requestedCourse);
	if (null == c)
	{
		ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
		return;
	}
	else if (null == c.name)
	{
		ErrorHandler.parseForErrors(Common.ENTRY_DOESNT_EXIST, response, null);
		return;		
	}
%>
	<div id="course_details" style="float:left">
	<b><u>Course name</u></b><br/>
	<p><%= c.name %></p>
	<b><u>Description</u></b><br/>
	<p><%= c.description %></p>
	<b><u>Credit points</u></b><br/>
	<p><%= c.creditPoints %></p>
	<b><u>Group number</u></b><br/>
	<p><%= c.groupNumber %></p>
	<b><u>Currently registered</u></b><br/>
	<p><%= c.registered %> ( <%= c.capacity %> )</p>
	</div>
	<div id="user_options" style="float:right">
<%
	String userType = (String) session.getAttribute("userType");
	boolean validStudent = false;
	boolean validAdmin = false;
	ArrayList<Course> allCourses = QueryDispatcher.getAllCourses();
	if (null == allCourses)
	{
		ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
		return;		
	}
	ArrayList<Course> userAvailableCourses = null;
	ArrayList<Course> userRegisteredCourses = null;
	ArrayList<Course> adminCreatedCourses = null;
		
	if (null != userType)
	{
		if (userType.equals(Common.studentType))
		{
			userAvailableCourses = QueryDispatcher.getAllAvailableCourses(username);
			userRegisteredCourses = QueryDispatcher.getRegisteredCourses(username);
			if (null == userAvailableCourses || null == userRegisteredCourses)
			{
				ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
				return;
			}
			validStudent = true;
		}
		
		if (userType.equals(Common.adminType))
		{
			adminCreatedCourses = QueryDispatcher.getAllGeneratedCourses(username);
			if (null == adminCreatedCourses)
			{
				ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);	
				return;
			}
			validAdmin = true;
		}
	}
	if (validStudent)
	{
		boolean userCanRegister = userAvailableCourses.contains(c);
		boolean userCanUnregister = userRegisteredCourses.contains(c);
		if (userCanRegister || userCanUnregister)
		{
%>
			<form method="get" action="CourseRegistration">
			<input type="hidden" name="course" value="<%= c.name %>"/>
<%
			if (userCanRegister)
			{
%>					
				<input type="hidden" name="operation" value="<%= "Register" %>"/>
				<input type="submit" value="Register" />	
<%
			}

			if (userCanUnregister)
			{
%>					
				<input type="hidden" name="operation" value="<%= "Unregister" %>"/>
				<input type="submit" value="Unregister" />	
<%		
			}
%>
			</form>
<%
		}
	}
	
	if (validAdmin)
	{
		if (adminCreatedCourses.contains(c))
		{
%>
			<form method="get" action="removeCourse">
			<input type="hidden" name="courseToRemove" value="<%= c.name %>"/>
			<input type="submit" value="Remove the course" />	
			</form>
<%
		}
	}
%>
	</div>
	<div id="course_sessions">
<%
	ArrayList<CourseSession> sessions = QueryDispatcher.getCourseSessions(requestedCourse);
	if (null == sessions || 0 == sessions.size())
	{
		ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
		return;
	}
	
	Collections.sort(sessions);
%>
	<b><u>Sessions</u></b><br/>
	<table style="text-align:center" border="2">
		<thead>
			<tr>
				<th><b>Day</b></th>
				<th><b>Hours</b></th>
			</tr>
		</thead>		
<%
	for (CourseSession s : sessions)
	{
%>
		<tbody>
			<tr>
				<td><%= Common.days[s.day] %></td>
				<td><%= s.startingTime %>:00 - <%= s.endingTime %>:00</td>
			</tr>
		</tbody>
<%
	}
%>
	</table>
	</div>
</div>
<jsp:include page="sidebar.jsp"/>
<jsp:include page="footer.html"/>
</body>
</html>